import { getList, upload, add, detail, update, del } from './service';

export default {
	namespace: 'graphic',
	state: {
		list: {
			pageData: [
				{
					id: 1,
					title: '习大大党建',
					img: '/',
					sector: 1,
					sender: '张三',
					view: 2000,
					sendDate: '2018-12-13',
				},
			],
			pageSize: 10,
			pageNum: 1,
			total: 20,
		},
		detail: {
			id: 1,
			title: '习大大党建',
			img: ['https://wx4.sinaimg.cn/mw690/005BwPSAly1fy1p2glssmj30u011laxd.jpg'],
			sector: 1,
			sender: '张三',
			view: 2000,
			sendDate: '2018-12-13',
			text: 'test',
		},
		search: {
			pageNum: 1,
			pageSize: 10,
			plateName: '',
			startDate: '',
			endDate: '',
			name: '',
		},
	},
	reducers: {
		save(state, { payload }) {
			return { ...state, ...payload };
		},
	},
	effects: {
		*getList({}, { call, put, select }) {
			const { pageNum, pageSize, plateName, startDate, endDate, name } = yield select(
				state => state.graphic.search
			);
			const res = yield call(getList, {
				pageNum,
				pageSize,
				plateName,
				startDate,
				endDate,
				name,
			});
			yield put({
				type: 'save',
				payload: {
					list: {
						pageData: res.pageData,
						pageNum: res.pageNum,
						pageSize: res.pageSize,
						total: res.total,
					},
				},
			});
		},
		*upload({ payload, callback }, { call }) {
			const res = yield call(upload, payload);
			if (callback) callback(res);
		},
		*add({ payload, callback }, { call }) {
			yield call(add, payload);
			if (callback) callback();
		},
		*detail({ payload, callback }, { call, put }) {
			const data = yield call(detail, payload);
			yield put({
				type: 'save',
				payload: {
					detail: data,
				},
			});
			if (callback) callback(data);
		},
		*update({ payload, callback }, { call }) {
			yield call(update, payload);
			if (callback) callback();
		},
		*delete({ payload, callback }, { call }) {
			yield call(del, payload);
			if (callback) callback();
		},
	},
};
